package com.fingard.net.ftp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Date;

import com.fingard.constant.Format;

public class BizFtpSocket {
	public enum ENFtpSocketType {
		ControlSocket,
		DataSocket
	}
	public enum ENFtpDataType{
        Content,
        DownloadFile,
        UploadFile
    }
	
	public String ServerHostPort;

    public byte[] ReceivedBytes;
    public byte[] BufferBytes;

    public int RecByteLength;
    public int RecZeroCount;

    public Date BeginRecTime;
    public Date LastRecTime;

    public boolean IsCompleted;

    /// <summary>
    /// 返回的Control提示数据传输是否已经完成
    /// </summary>
    //public bool IsTrsfCmpltPASV;

    public Socket FtpSocket;
    public ENFtpSocketType FtpSocketType;
    public ENFtpDataType FtpDataType;

    public BizFtpSocket(ENFtpSocketType p_FtpSocketType, ENFtpDataType p_FtpDataType)
    {
        FtpSocketType = p_FtpSocketType;
        FtpDataType = p_FtpDataType;
        _DestFileStream = null;
        ServerHostPort = "";
    }

    public void ReInit() throws IOException
    {
        IsCompleted = false;
        //IsTrsfCmpltPASV = false;
        RecByteLength = 0;
        RecZeroCount = 0;
        BeginRecTime = new Date();
        LastRecTime = new Date();
        ReceivedBytes = new byte[0];
        if (FtpDataType == ENFtpDataType.Content)
        {
            BufferBytes = new byte[10240];
        }
        else
        {
            BufferBytes = new byte[204800];
        }
        FileSize = 0;
        LastLogProgressDownFile = 0;
        LastLogTime = new Date();
        closeDestFileStream();
    }

    public long FileSize;

    public String DestLocalFile;
    private FileOutputStream _DestFileStream;

    public double getProgressDownFile(){
            if (FileSize > 0){
                return RecByteLength / FileSize;
            }else{
                return 0;
            }
    }
    
    public String getProgressDownFilePercent(){
        return Format.DecimalAmtFormat.format((getProgressDownFile() * 100)) + "%";
    }
    public double LastLogProgressDownFile;
    
    public Date LastLogTime;

    public void saveToDestFileStream(int p_WriteCount) throws IOException{
        if (_DestFileStream == null){
        	File dFile = new File(DestLocalFile);
        	if(!dFile.exists()){
        		dFile.createNewFile();
        	}
            _DestFileStream = new FileOutputStream(dFile, false);
        }
        _DestFileStream.write(BufferBytes, 0, p_WriteCount);
    }

    public void closeDestFileStream() throws IOException{
        if (_DestFileStream != null){
            _DestFileStream.close();
            _DestFileStream = null;
        }
    }

    public void close() throws IOException
    {
        closeDestFileStream();
        if (FtpSocket != null){
            FtpSocket.close();
            FtpSocket = null;
        }
        ReceivedBytes = null;
        BufferBytes = null;
    }
}
